Report with ZK Jasperreport Component

From Documentation
DocumentationSmall Talks2008JanuaryReport with ZK Jasperreport Component
Report with ZK Jasperreport Component

Author
Grace Lin, Engineer, Potix Corporation
Date
January 18, 2008
Version
Applicable to ZK 3.0.2 Freshly (zk-bin-3.0.2-FL-2008-01-17 and later)


Introduction

JasperReports is an open source Java reporting tool that can write to screen, to a printer or into PDF files. It can be used in Java-enabled applications, including J2EE or Web applications, to generate dynamic content. Before this article, we have ever show report in ZK by writing ZK glue codes (Report with ZK), but it is more difficult than directly using a component. Now we can use the report component to do this easily.


Usage of JasperReport Component

The following is the example code to use the report component. The CustomDataSource class is a data source which implements JRDataSource.

<?xml version="1.0" encoding="utf-8"?>

<window title="jasperreport demo" border="normal" height="100%">
    <borderlayout height="100%">
        <north maxsize="24" size="24" border="0">
            <button label="Report!" onClick="showReport()" />
        </north>
        <center border="none" flex="true">
            <jasperreport id="report" />
        </center>
    </borderlayout>
    <zscript><![CDATA[
        import java.util.*;
        
        void showReport() {
            //Preparing parameters
            Map parameters = new HashMap();
            parameters.put("ReportTitle", "Address Report");
            parameters.put("DataFile", "CustomDataSource.java");
            
            report.setSrc("DataSourceReport.jasper");
            report.setParameters(parameters);
            report.setDatasource(new CustomDataSource());
        }
    ]]></zscript>
</window>

The end user request a report by clicking the button and the "onClick" event handler will call showReport(), the report component will be set source path to get the PDF file and show on the page.

If you want to show report directly, you can set template source on the report component's attributes. Then, also set the data source and parameters of the report, like the following code.

<?xml version="1.0" encoding="utf-8"?>

<window title="jasperreport demo" border="normal" height="100%">
    <zscript>
        Map parameters = new HashMap();
        parameters.put("ReportTitle", "Address Report");
        parameters.put("DataFile", "CustomDataSource.java");
        
        CustomDataSource ds = new CustomDataSource();
    </zscript>
        
    <jasperreport src="DataSourceReport.jasper"
        parameters="${parameters}" datasource="${ds}" />
</window>

Jasperreport.png

Download

Conclusion

ZK developers can show report directly by using report component now. Set the template source, data source and parameters as same as what you do with JasperReports, then the component can show it easily. If you have any question, please feel free to leave comment here or post to ZK forum.

See Also

Open JasperReport File in new Window of webBrowser



Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.